package org.ecloud.api.provider;

import java.util.List;

import javax.annotation.Resource;

import org.beetl.sql.core.engine.PageQuery;
import org.ecloud.biz.api.model.ApiPo;
import org.ecloud.biz.api.service.ApiQueryService;
import org.ecloud.common.constants.Status;
import org.ecloud.core.entity.APIResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags = "服务管理", value = "服务获取")
@RestController
@RequestMapping("/")
public class ApiProvider {

	private Logger logger = LoggerFactory.getLogger(getClass());
	@Resource
	private ApiQueryService apiQueryService;
	
	@ApiOperation(value = "获取服务列表", notes = "无参获取所有服务数据")
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public APIResult<List<ApiPo>> list(){
		APIResult<List<ApiPo>> result = new APIResult<>(Status.SUCCESS.code());
		try{
			List<ApiPo> datas = apiQueryService.findAll();
			result.setData(datas);
		}catch(Exception e){
			result.setCode(Status.FAILURE_API_FINDALL.code());
			result.setMessage(Status.FAILURE_API_FINDALL.text());
			result.setCause(e.getMessage());
			logger.error(Status.FAILURE_API_FINDALL.text(), e);
		}
		
		return result;
	}
	
	@ApiOperation(value = "分页获取服务列表", notes = "分页获取服务数据")
	@RequestMapping(value = "list/page", method = RequestMethod.GET)
	public APIResult<PageQuery<ApiPo>> listPage(
			@ApiParam(name = "page", value = "页码", required = true) @RequestParam(name = "page", required = true) long page, 
			@ApiParam(name = "limit", value = "页容量", required = true) @RequestParam(name = "limit", required = true) long limit){
		APIResult<PageQuery<ApiPo>> result = new APIResult<>(Status.SUCCESS.code());
		try{
			PageQuery<ApiPo> pageQuery = new PageQuery<ApiPo>(page, limit);
			apiQueryService.query(pageQuery);
			result.setData(pageQuery);
		}catch(Exception e){
			result.setCode(Status.FAILURE_API_QUERY.code());
			result.setMessage(Status.FAILURE_API_QUERY.text());
			result.setCause(e.getMessage());
			logger.error(Status.FAILURE_API_QUERY.text(), e);
		}
		
		return result;
	}
	
}
